package com.daoeping.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.daoeping.domain.Bsc_grade;
import com.daoeping.domain.Bsc_skill;
import com.daoeping.domain.Bsc_workyear;
import com.daoeping.domain.Usr_skill_detail;
import com.daoeping.domain.Usr_userProduction;
import com.daoeping.service.GradeService;
import com.daoeping.service.SkillService;
import com.daoeping.service.UserProductionService;
import com.daoeping.service.UsrSkillDetailService;
import com.daoeping.service.Bsc_workyearService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class UserProductionAction extends ActionSupport{

	private UserProductionService usrProductionService;
	private Usr_userProduction usr_userProduction;
	private Usr_skill_detail usr_skill_detail;
	
	private UsrSkillDetailService usrSkillDetailService;
	private SkillService skillService;
	private GradeService gradeService;
	private Bsc_workyearService workYearService;
	
	List skillid ,gradeid,workyearid,skilldescription;
	
	public UserProductionService getUsrProductionService() {
		return usrProductionService;
	}

	public void setUsrProductionService(UserProductionService usrProductionService) {
		this.usrProductionService = usrProductionService;
	}
	
	public Usr_userProduction getUsr_userProduction() {
		return usr_userProduction;
	}

	public void setUsr_userProduction(Usr_userProduction usr_userProduction) {
		this.usr_userProduction = usr_userProduction;
	}
	
	public Usr_skill_detail getUsr_skill_detail() {
		return usr_skill_detail;
	}

	public void setUsr_skill_detail(Usr_skill_detail usr_skill_detail) {
		this.usr_skill_detail = usr_skill_detail;
	}
	
	public UsrSkillDetailService getUsrSkillDetailService() {
		return usrSkillDetailService;
	}

	public void setUsrSkillDetailService(UsrSkillDetailService usrSkillDetailService) {
		this.usrSkillDetailService = usrSkillDetailService;
	}
	
	/*Set,get method of Service*/
	public SkillService getSkillService() {
		return skillService;
	}

	public void setSkillService(SkillService skillService) {
		this.skillService = skillService;
	}
	
	public GradeService getGradeService() {
		return gradeService;
	}

	public void setGradeService(GradeService gradeService) {
		this.gradeService = gradeService;
	}
	
	public Bsc_workyearService getWorkYearService() {
		return workYearService;
	}

	public void setWorkYearService(Bsc_workyearService workYearService) {
		this.workYearService = workYearService;
	}

	/*Set,get method for Usr_skill Lists*/
	public List getSkillid() {
		return skillid;
	}

	public void setSkillid(List skillid) {
		this.skillid = skillid;
	}

	public List getGradeid() {
		return gradeid;
	}

	public void setGradeid(List gradeid) {
		this.gradeid = gradeid;
	}

	public List getWorkyearid() {
		return workyearid;
	}

	public void setWorkyearid(List workyearid) {
		this.workyearid = workyearid;
	}

	public List getSkilldescription() {
		return skilldescription;
	}

	public void setSkilldescription(List skilldescription) {
		this.skilldescription = skilldescription;
	}	
	
	public String AddUserProduction() throws Exception{
		int i,mySkillid,myGradeid,myWorkyearid;
		String mySkilldescription;
		List<Usr_skill_detail> skillList = new ArrayList();
		
		this.usrProductionService.addUserProduction(usr_userProduction);		
		
		if(!skillid.isEmpty()){
			for(i=0;i<skillid.size();i++){
				
				mySkillid          =  Integer.parseInt(skillid.get(i).toString());
				myGradeid          =  Integer.parseInt(gradeid.get(i).toString());
				myWorkyearid       =  Integer.parseInt(workyearid.get(i).toString());
				mySkilldescription =  skilldescription.get(i).toString();				

				Bsc_skill ss = (Bsc_skill) this.skillService.findBsc_skillById(mySkillid).get(0);
				Bsc_grade gg = (Bsc_grade) this.gradeService.findBsc_gradeById(myGradeid).get(0);
		/*		Bsc_workyear ww = (Bsc_workyear) this.workYearService.findBsc_workyearById(myWorkyearid).get(0);

				Usr_skill_detail usr_skill_detail = new Usr_skill_detail();
				usr_skill_detail.setBsc_skill(ss);
				usr_skill_detail.setBsc_grade(gg);
				usr_skill_detail.setBsc_workyear(ww);
				usr_skill_detail.setDescription(mySkilldescription);				
				usr_skill_detail.setUsr_userProduction(usr_userProduction);
*/
				this.usrSkillDetailService.addUsrSkillDetail(usr_skill_detail);
				skillList.add(usr_skill_detail);
				
			}
		}	
		
		Usr_userProduction myUsr_userProduction = ((List<Usr_userProduction>) this.usrProductionService.findUserProductionByID(usr_userProduction.getId())).get(0);

		ActionContext.getContext().put("myUsr_userProduction", myUsr_userProduction);
		ActionContext.getContext().put("myUserSkillDetailList", skillList);

		return "success";
		
	}
	
	public String getUserProductionByUserId(){
		int userid = usr_userProduction.getUsr_user().getId();	
		
		Usr_userProduction myUsr_userProduction = ((List<Usr_userProduction>)this.usrProductionService.findUserProductionByUserID(userid)).get(0);
		ActionContext.getContext().put("myUsr_userProduction", myUsr_userProduction);
		
		int  userproduction_id = myUsr_userProduction.getId();
		List<Usr_skill_detail> myUserSkillDetailList = this.usrSkillDetailService.findAllUsrSkillDetailByUserProductionID(userproduction_id);
		
		ActionContext.getContext().put("myUserSkillDetailList", myUserSkillDetailList);		
		return "success";
	}
	
	public String testProduction()throws Exception{
		return "test";
	}
	
	public void setSession(Map<String, Object> sessionMap) {		
		
	}	
	
	protected Map<String,Object> getSession(){
		return  (Map<String,Object>)ActionContext.getContext().getSession();
	}
	
	protected HttpServletRequest getRequest(){
		return ServletActionContext.getRequest (); 
	}
}
